﻿using Restaurant.DatabaseTools;
using Restaurant.entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Restaurant
{
    public partial class NewTable : Form
    {
        public bool isroom = false;
        public int roomidint;
        public int state = 0;
        public double sumCost;

       

        //1:完全新的  0：已存在的
        public NewTable(string roomid_t,bool isr,int ste)
        {
            InitializeComponent();
            roomid.Text = "第" + roomid_t + "桌管理";
            roomidint = Convert.ToInt32(roomid_t);
            this.isroom = isr;
            this.state = ste;
        }

        private void NewTable_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Left = 0;
            this.Top = 0;
       

            int tp = 9;
            for(int i=0;i<SData.db.MDB.Count();i++)
            {
                menuC m = new menuC();
                m.name = SData.db.MDB[i].itemName;
                m.price = SData.db.MDB[i].itemPrice.ToString();
                m.picindex = SData.db.MDB[i].itemNum;
                m.Location=new Point(13,tp);
                m.tableid =roomidint-1;
                m.Size = new Size(560, 208);
                panel1.Controls.Add(m);
                tp += 225;
            }

            //MessageBox.Show();
            if(state==1)
            {
                CpeopleComboBox.Text = "李勇英";
                GuestComboBox2.Text = "1";
                string id = Tools.GetRandomString(9, true, true, true, false, "g");
                SData.newGusing(id, Convert.ToInt32(GuestComboBox2.Text), CpeopleComboBox.Text, remarksTextbox.Text, Convert.ToInt32(Gcost.Text), isroom, false);
                SData.table[roomidint-1] = id;//更新桌子对应的订单
                state = 0;
                
            }
            else
            {          
                state = 0;
                CpeopleComboBox.Text = SData.Glistusing[SData.Getindex(SData.table[roomidint - 1])].chargePeople;
                GuestComboBox2.Text = SData.Glistusing[SData.Getindex(SData.table[roomidint - 1])].guestNum.ToString();
                remarksTextbox.Text = SData.Glistusing[SData.Getindex(SData.table[roomidint - 1])].remarks;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("保存订单吗？未保存的订单数据将会丢失", "是否保存", messButton);
            if (dr == DialogResult.OK)
            {
                SData.Modify_Gusing(SData.Getindex(SData.table[roomidint - 1]), Convert.ToInt32(GuestComboBox2.Text), CpeopleComboBox.Text, remarksTextbox.Text, Convert.ToInt32(Gcost.Text), isroom, false);
            }
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("确定要删除订单吗?", "删除订单", messButton);
            if (dr == DialogResult.OK)
            {
                SData.Glistusing[SData.Getindex(SData.table[roomidint - 1])].isSettlement = true;
                this.Close();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
             SData.Modify_Gusing(SData.Getindex(SData.table[roomidint-1]), Convert.ToInt32(GuestComboBox2.Text), CpeopleComboBox.Text, remarksTextbox.Text, Convert.ToInt32(Gcost.Text), isroom, false);
            
        }


        public void refreshMenuList()
        {
            Menlist.Items.Clear();
            sumCost = Convert.ToDouble(Gseats.Text);
            for(int i=0;i<SData.Glistusing[SData.Getindex(SData.table[roomidint-1])].menuitems.Count();i++)
            {
                Menlist.Items.Add(SData.Glistusing[SData.Getindex(SData.table[roomidint-1])].menuitems[i].itemName);
                sumCost += SData.Glistusing[SData.Getindex(SData.table[roomidint - 1])].menuitems[i].itemPrice;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Gchargepeople.Text = CpeopleComboBox.Text;
            Gguestnum.Text = GuestComboBox2.Text;
            Gseats.Text = (Convert.ToInt32(GuestComboBox2.Text) * 5).ToString();
            Gremarks.Text = remarksTextbox.Text;
            Gmeunitemssum.Text = Menlist.Items.Count.ToString();
            Gcost.Text = sumCost.ToString();
            refreshMenuList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("确定要结算吗?", "结算订单", messButton);
            if (dr == DialogResult.OK)
            {
                SData.Glistusing[SData.Getindex(SData.table[roomidint - 1])].isSettlement = true;
                this.Close();
            }
        }
    }
}
